import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_scaffold/common/index.dart';
import 'package:oramemo/common/config/app_config.dart';
import 'package:oramemo/common/langs/text_key.dart';
import 'package:oramemo/common/routes/routes.dart';
import 'package:oramemo/common/utils/extension_util.dart';
import 'package:oramemo/core/config/api_config.dart';
import 'package:oramemo/core/controllers/auth_controller.dart';
import 'package:oramemo/generated/assets.dart';
import 'package:oramemo/pages/setting/setting_router.dart';
import 'package:oramemo/pages/setting/widgets/update_dialog.dart';
import 'package:oramemo/pages/test/test_logic.dart';
import 'package:oramemo/pages/user/user_router.dart';

class PersonalCenterPage extends StatelessWidget {
  const PersonalCenterPage({super.key});

  @override
  Widget build(BuildContext context) {
    final logic = AuthController.to;

    return RefreshIndicator(
      onRefresh: () async {
        logic.getUserInfo();
      },
      child: ListView(
        children: [
          // Status Bar and Profile Section
          Padding(
            padding: const EdgeInsets.fromLTRB(16.0, 58, 16, 16),
            child: InkWell(
              onTap: () {
                Get.toNamed(SettingRouter.profile);
              },
              child: Obx(() {
                return Row(
                  children: [
                    const SizedBox(width: 2),
                    Container(
                      width: 58,
                      height: 58,
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        border: Border.all(
                          color: Colors.white,
                          width: 2,
                        ),
                        image: const DecorationImage(
                          image: AssetImage(Assets.imagesHead),
                          fit: BoxFit.cover,
                        ),
                      ),
                      child: logic.user?.avatar?.isNotEmpty == true
                          ? CircleAvatar(
                              radius: 28,
                              backgroundImage:
                                  NetworkImage(logic.user!.avatar!.toFullUrl()),
                            )
                          : null,
                    ),
                    const SizedBox(width: 12),
                    Text(
                      logic.user?.nickName ?? TextKey.personalNickNameDefault.tr,
                      style: const TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const Spacer(),
                    IconButton(
                      icon: const Icon(
                        Icons.settings_outlined,
                        size: 24,
                      ),
                      onPressed: () {
                        Get.toNamed(SettingRouter.profile);
                      },
                    ),
                  ],
                );
              }),
            ),
          ),

          // Account Balance Card
          Container(
            margin: const EdgeInsets.symmetric(horizontal: 16),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8),
            ),
            child: ListTile(
              title: Text(
                TextKey.personalRemainingTime.tr,
                style: const TextStyle(
                  color: Colors.black54,
                  fontSize: 16,
                ),
              ),
              trailing: Text(
                TextKey.personalRemainingTimeValue.tr,
                style: TextStyle(
                  color: Colors.blue[600],
                  fontSize: 18,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
          ),

          // Feature Buttons
          Padding(
            padding: const EdgeInsets.fromLTRB(16, 22, 16, 26),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                _buildFeatureButton(Assets.personalRole, TextKey.personalTranscriptionRecord.tr,
                    () => Get.toNamed(UserRouter.transcriptionList)),
                _buildFeatureButton(Assets.personalCard, TextKey.personalConsumptionRecord.tr,
                    () => Get.toNamed(UserRouter.consumptionRecord)),
                _buildFeatureButton(
                    Assets.personalGpt,
                    TextKey.personalHelpCenter.tr,
                    () => Get.toNamed(Routers.webView,
                        arguments: {'title': TextKey.personalHelpCenter.tr, 'url': AppConfig.webUrl})),
              ],
            ),
          ),

          // Settings List
          Container(
            margin: const EdgeInsets.symmetric(horizontal: 16),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(12),
            ),
            child: Column(
              children: [
                _buildSettingsItem(Assets.personalVersion, TextKey.personalSubscription.tr,
                    onTap: () => Get.toNamed(UserRouter.subscription)),
                _buildSettingsItem(Assets.personalUser, TextKey.personalWhitelist.tr,
                    onTap: () => Get.toNamed(UserRouter.userManagement)),
                _buildSettingsItem(Assets.personalLanguage, TextKey.personalLanguage.tr,
                    onTap: () => Get.toNamed(SettingRouter.language)),
                _buildSettingsItem(Assets.personalOta, TextKey.personalOtaUpdate.tr,
                    onTap: () => _showUpdateDialog(context)),
                _buildSettingsItem(Assets.personalAbout, TextKey.personalAboutUs.tr,
                    onTap: () => Get.toNamed(SettingRouter.about)),
                if (isDebugMode)
                  _buildSettingsItem(Assets.personalAbout, 'test',
                      onTap: () => testTo()),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildFeatureButton(
      String assetPath, String label, Function()? onTap) {
    return SizedBox(
      width: Get.width * 0.28,
      child: Column(
        children: [
          InkWell(onTap: onTap, child: IconX.image(assetPath, size: 46)),
          const SizedBox(height: 8),
          Text(
            label,
            style: const TextStyle(
              fontSize: 14,
              color: Colors.black87,
            ),
            textAlign: TextAlign.center,
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
          ),
        ],
      ),
    );
  }

  Widget _buildSettingsItem(String assetPath, String label,
      {VoidCallback? onTap}) {
    return InkWell(
      onTap: onTap,
      child: Container(
        height: 54,
        margin: const EdgeInsets.symmetric(horizontal: 12),
        decoration: const BoxDecoration(
          border: Border(bottom: BorderSide(color: Colors.black12, width: 0.5)),
        ),
        child: Row(children: [
          const SizedBox(width: 3),
          IconX.image(assetPath, size: 20),
          const SizedBox(width: 4),
          Expanded(
            child: Text(
              label,
              style: const TextStyle(
                fontSize: 16,
                color: Colors.black87,
              ),
            ),
          ),
          const Icon(
            Icons.chevron_right,
            color: Colors.black26,
          ),
        ]),
      ),
    );
  }

  void _showUpdateDialog(BuildContext context) {
    showToast(TextKey.refresh.tr);
    // showDialog<void>(
    //     context: context,
    //     barrierDismissible: false,
    //     builder: (_) => const UpdateDialog());
  }
}
